/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 *  
 */
public class ConfigurationModel extends AbstractModel
{
	protected int groupId;
	protected int id;
	protected String title;
	protected String key;
	protected String value;
	protected String description;
	protected String format;
	protected List<ValueModel> values;
	protected Date dateAdded;
	protected Date lastModified;
	
	public ConfigurationModel(JSONObject jsonObject)
	{
		values = new ArrayList<ValueModel>();
		update(jsonObject);
	}
	
	public void update(JSONObject jsonObject)
	{
		update();
		groupId = convertToInteger(jsonObject, "group_id");
		id = convertToInteger(jsonObject, "id");
		title = convertToString(jsonObject, "title");
		key = convertToString(jsonObject, "key");
		description = convertToString(jsonObject, "description");
		value = convertToString(jsonObject, "value");
		format = convertToString(jsonObject, "format");
		dateAdded = convertToDate(jsonObject, "date_added");
		lastModified = convertToDate(jsonObject, "last_modified");
		
		buildValues(jsonObject);
	}

	protected void buildValues(JSONObject jsonObject)
	{
		values.clear();

		try {
			JSONValue listValue = jsonObject.get("values");

			if (listValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = listValue.isArray()) != null)
				{
					int sz = jsonArray.size();		
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonAObject;
						if ((jsonAObject = jsonValue.isObject()) != null)
						{
							ValueModel valueModel = new ValueModel(jsonAObject);
							values.add(valueModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}

	public void update()
	{
		groupId = 0;
		id = 0;
		title = "";
		key = "";
		value = "";
		description = "";
		format = "";
		dateAdded = null;
		lastModified = null;
	}

	public int getGroupId() {
		return groupId;
	}

	public int getId() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public String getKey() {
		return key;
	}

	public String getValue() {
		return value;
	}

	public String getDescription() {
		return description;
	}

	public String getFormat() {
		return format;
	}

	public List<ValueModel> getValues() {
		return values;
	}

	public Date getDateAdded() {
		return dateAdded;
	}

	public Date getLastModified() {
		return lastModified;
	}

}
